import { createTree, ResTreeVO, TreeDataSource } from './entity';
import { Data } from 'ahooks/lib/usePagination/types';
import { request } from 'ice';

export interface QueryParams {
  /** 邮箱或者名称 */
  keywords?: string;
}

export interface ListVO {
  id?: number;
  /** 名称 */
  name?: string;
  /** 邮箱 */
  email?: string;
  /** 密码 */
  password?: string;
  /** 卖家id */
  sellerId?: number;
  /** 状态 */
  status?: number;
  createTime?: number;
}

export interface PData extends Data {
  list: ListVO[];
}

/** 列表 */
export const userList = (pageParams: ICommon.PageParams, formData: QueryParams): Promise<PData> => {
  const params = {
    ...pageParams,
    ...formData,
  };
  return request.get<ICommon.Pagination<ListVO>>('/erp/user/list', { params }).then((res) => ({
    total: res?.total || 0,
    list: res?.data || [],
  }));
};

/**
 * 保存账户信息
 * @param params
 * @returns
 */
export const saveUser = (params: ListVO): Promise<any> => {
  return request.post('/erp/user/save', { ...params });
};

/**
 * 删除账号
 * @param id
 * @returns
 */
export const deleteUser = (userId?: number): Promise<any> => {
  return request.get('/erp/user/delete', { params: { userId } });
};
/** 禁用启用账号 */
export const enableOrUnableUser = (userId: number, enable?: boolean): Promise<any> => {
  return request.get('/erp/user/enable', { params: { userId, enable } });
};

export interface RoleListVO {
  id: number;
  name: string;
  type?: number;
  code?: string;
  status?: number;
  createTime?: number;
}

/**
 * 获取角色列表
 * @returns
 */
export const queryRoleList = (): Promise<TreeDataSource[]> => {
  return request
    .get<RoleListVO[]>('/erp/role/select/list')
    .then((res) => res?.map((t) => ({ label: t.name, key: `${t.id}` })));
};

export interface UserRole {
  userId: number;
  roleId: number;
  name: string;
  code: string;
}

/** 查询用户角色授权列表 */
export const queryUserRole = (userId?: number): Promise<number[]> => {
  return request
    .get<UserRole[]>('/erp/user/role/list', { params: { userId } })
    .then((res) => res?.map((t) => t.roleId));
};

export interface SaveUserRoleAuthParam {
  userId: number;
  roleIds: number[];
}

/** 保存角色授权 */
export const saveUserRole = (params: SaveUserRoleAuthParam): Promise<any> => {
  return request.post('/erp/user/role/save', { ...params });
};
